package org.jeecg.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse;
import com.taobao.api.ApiException;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.modules.dinguser.entity.DiyDingUser;
import org.jeecg.modules.dinguser.mapper.DiyDingUserMapper;
import org.jeecg.modules.mapper.DiyProfessorInfoMapper;
import org.jeecg.modules.util.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ZhangHaoyu
 * @description
 * @date 2020/8/3 10:52
 */
@Service
public class DingApiServiceImpl implements DingApi {

    @Autowired
    private DiyProfessorInfoMapper diyProfessorInfoMapper;

    @Autowired
    private DiyDingUserMapper diyDingUserMapper;

    @Override
    public String selUserIdByUnionid(String username) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getUseridByUnionid");
            OapiUserGetUseridByUnionidRequest req = new OapiUserGetUseridByUnionidRequest();

            req.setUnionid(username);
            req.setHttpMethod("GET");
            OapiUserGetUseridByUnionidResponse rsp = client.execute(req, AccessTokenUtil.getToken());
            return rsp.getBody();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getUnionIdByUserId(String originator_userid) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest req = new OapiUserGetRequest();
        req.setUserid(originator_userid);
        req.setHttpMethod("GET");
        OapiUserGetResponse rsp = null;
        try {
            rsp = client.execute(req,AccessTokenUtil.getToken());
            JSONObject object = JSONObject.parseObject(rsp.getBody());
            // 得到unionid（username）
            String unionid = object.getString("unionid");
            // 获得Id
            return unionid;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public String getUnionIdByUserIdFromDB(String originator_userid) {
        return diyProfessorInfoMapper.getUnionIdByUserId(originator_userid);
    }


    @Override
    public void saveDingUser(String userId){
        //根据userid获取用户信息
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userId);
        request.setHttpMethod("GET");
        OapiUserGetResponse response = null;
        try {
            response = client.execute(request, AccessTokenUtil.getToken());
        } catch (ApiException e) {
            e.printStackTrace();
        }
        //保存到diy_ding_user表中去
        DiyDingUser dingUser = new DiyDingUser();
        dingUser.setDingUserId(userId);
        dingUser.setDingMobile(response.getMobile());
        dingUser.setDingUnionid(response.getUnionid());
        dingUser.setDingEmail(response.getEmail());
        dingUser.setDingDepartment(response.getDepartment().toString());
        diyDingUserMapper.insert(dingUser);
    }
}
